# 定义索引类
from haystack import indexes

from .models import Goods


class GoodsIndex(indexes.SearchIndex, indexes.Indexable):
    """
    商品模型索引类:
    索引类名: <模型类>+Index
    """
    # document=True：说明text字段是索引字段
    # use_template=True：说明索引字段中包含哪些内容会在一个文件中进行指定
    text = indexes.CharField(document=True, use_template=True)
    id = indexes.IntegerField(model_attr='id')
    name = indexes.CharField(model_attr='name')
    goods_front_image = indexes.CharField(model_attr='goods_front_image')
    market_price = indexes.FloatField(model_attr='market_price')
    shop_price =  indexes.FloatField(model_attr='shop_price')
    sold_num = indexes.IntegerField(model_attr='sold_num')


    def get_model(self):
        """返回索引类对应模型类"""
        return Goods


    def index_queryset(self, using=None):
        """返回要建立索引数据的查询集"""
        return self.get_model().objects.filter(goods_status=1,is_delete=False)